<template>
  <el-container class="home_container">
    <!--头部区域  -->
    <el-header>
      <div>
        <img src="../assets/img/圣诞帽.png" alt="" />
        <h1>电商后台管理系统</h1>
      </div>
      <el-button type="info" plain @click="logout">退出登录</el-button>
    </el-header>
    <!--主体区域  -->
    <el-container class="main_container">
      <!-- 边侧栏区域 -->
      <el-aside :width="collapse ? '64px' : '200px'">
        <div class="collapse" @click="isCollapse">|||</div>
        <el-menu
          background-color="white"
          text-color="rgb(44, 17, 13)"
          active-text-color="rgb(9, 88, 16)"
          :unique-opened="true"
          :collapse="collapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconPic[item.id]"></i>
              <span class="title">{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + i.path"
              v-for="i in item.children"
              :key="i.id"
              @click="saveNavState('/' + i.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ i.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体内容区域 -->
      <el-main>
        <!-- welcome 路由出口 -->
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  props: {},
  data() {
    return {
      menulist: [],
      iconPic: {
        125: "iconfont icon-users",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      // * 是否折叠菜单
      collapse: false,
      // * activePath
      activePath:'',
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    //   todo 退出登录功能
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/Login");
    },
    // todo 获取左侧菜单数据
    getMenuList() {
      this.$http.get("menus", {}).then((res) => {
        // * 请求失败抛出错误
        if (res.data.meta.status != 200) {
          throw res.data.meta.msg;
        }
        //* 请求成功 拿到数据放在data中
        this.menulist = res.data.data;
      });
    },
    // todo 折叠菜单
    isCollapse() {
      this.collapse = !this.collapse;
    },
    // todo 保存链接的激活状态 传入的参数是index值 后台获取到的自己的路径 把路径存储进session
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath);
      this.activePath = activePath;
    }
  },
  components: {},
};
</script>

<style scoped lang="less">
.home_container {
  width: 100%;
  height: 100%;
  .el-header {
    width: 100%;
    height: 15%;
    background-color: rgb(36, 102, 47);
    display: flex;
    justify-content: space-between;
    align-items: center;
    div {
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        width: 50px;
      }
      h1 {
        color: white;
        margin-left: 15px;
      }
    }
  }
  .main_container {
    width: 100%;
    height: 75%;
    .el-aside {
      // background-color: rgb(245, 205, 96);
      background-color: rgb(168, 41, 18);
      .el-menu {
        border: none;
      }
      .title {
        margin-left: 10px;
      }
      .collapse {
        text-align: center;
        color: white;
        letter-spacing: 4px;
        font-size: 20px;
        padding: 5px 0;
        cursor: pointer;
      }
    }
    .el-main {
      background-color: rgb(243, 248, 244);
    }
  }
}
</style>
